package com.coocaa.ops.api.v1.internal.provider.impl;

import com.coocaa.ops.api.base.cache.enums.InterfaceCacheMethodEnum;
import com.coocaa.ops.api.base.cache.model.SimpleCacheKey;
import com.coocaa.ops.api.base.cache.service.SimpleCacheService;
import com.coocaa.ops.api.common.constant.PolicyGroupCategory;
import com.coocaa.ops.api.v1.home.biz.IModuleBiz;
import com.coocaa.ops.api.v1.home.handler.StoreModeHandler;
import com.coocaa.ops.api.v1.internal.mapper.IModuleCacheMapper;
import com.coocaa.ops.api.v1.internal.provider.ICacheUpdateProvider;
import com.coocaa.ops.common.core.biz.cache.constant.CacheTime;
import com.coocaa.ops.common.core.biz.dmp.entity.DmpRegistryRlsInfoEntity;
import com.coocaa.ops.common.core.biz.homepage.entity.HomeModuleContentConfigInfo;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author tangminghong
 * @date 2020-08-11
 */
@Service("module")
public class ModuleCacheUpdateProviderImpl implements ICacheUpdateProvider {

    @Autowired
    private SimpleCacheService cacheService;

    @Autowired
    private HomepageCacheUpdateProviderImpl homepageCacheUpdateProvider;

    @Autowired
    private IModuleCacheMapper moduleCacheMapper;

    @Autowired
    private IModuleBiz moduleBiz;

    /**
     * 更新缓存-通过资源ID
     *
     * @param idStr
     * @return 缓存key列表
     */
    @Override
    public List<String> updateCacheByResourceId(String idStr) {
        Integer id = Integer.valueOf(idStr);
        //获取模块关联的首页
        List<Integer> homepageIds = moduleCacheMapper.getHomepageIdListByModuleId(id);
        List<Integer> homepageIdsForModuleDmp = moduleCacheMapper.getHomepageIdListByModuleIdForModuleDmp(id);
        List<String> finalKeys = new ArrayList<>(homepageIds.size());
        if (CollectionUtils.isNotEmpty(homepageIds)) {
            for (Integer homepageId : homepageIds) {
                List<String> keys = homepageCacheUpdateProvider.updateCacheByResourceId(homepageId.toString());
                finalKeys.addAll(keys);
            }
        }
        if (CollectionUtils.isNotEmpty(homepageIdsForModuleDmp)) {
            for (Integer homepageId : homepageIdsForModuleDmp) {
                List<String> keys = homepageCacheUpdateProvider.updateCacheByResourceId(homepageId.toString());
                finalKeys.addAll(keys);
            }
        }
        handleStoreMode(id, finalKeys);
        handleTabDmp(id, finalKeys);
        return finalKeys;
    }

    private void handleStoreMode(Integer id, List<String> cacheKeys) {
        if (StoreModeHandler.MODULE_ID.equals(id)) {
            HomeModuleContentConfigInfo module = moduleBiz.getModuleByIdAndVersion(id, null);
            if (module != null) {
                SimpleCacheKey cacheKey = SimpleCacheKey.getCacheKeyInstance(InterfaceCacheMethodEnum.getStoreModeModule)
                        .withFilterKey("moduleId", id);
                cacheService.set(cacheKey, module, CacheTime.ONE_DAY);
                cacheKeys.add(cacheKey.toString());
            }
        }
    }

    private void handleTabDmp(Integer id, List<String> cacheKeys) {
        List<DmpRegistryRlsInfoEntity> dmpInfo = moduleBiz.getModuleTabDmpInfo(id, PolicyGroupCategory.FORMAL, null);
        if (dmpInfo != null) {
            SimpleCacheKey cacheKey = SimpleCacheKey.getCacheKeyInstance(InterfaceCacheMethodEnum.getDmpTabList)
                    .withFilterKey("moduleId", id);
            cacheService.set(cacheKey, dmpInfo,  CacheTime.ONE_DAY);
            cacheKeys.add(cacheKey.toString());
        }
    }
}
